﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Threading;

namespace AppDemo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //var box = new MessageWindowDialog();
            //box.MsgBoxImage = MessageBoxImage.Error;
            //box.MsgBoxButton = MessageBoxButton.OK;
            //box.MsgBoxContent = @"出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了出错了";
            ////box.AutoClose = true;
            //var a = box.Show();
           // MessageBox.Show("xxx", "", MessageBoxButton.OK, MessageBoxImage.Question);
            //var a=new Window4();
            //a.Show();
            //ApplicationUtil.DoEvents();
            //new Window2().Show();
            //a.Close();
          //  new Window3().ShowDialog();
            base.OnStartup(e);
        }
    }

    public class ApplicationUtil
    {
        static readonly DispatcherOperationCallback exitFrameCallback = ExitFrame;

        /// <summary>
        /// Processes all UI messages currently in the message queue.
        /// </summary>
        public static void DoEvents()
        {
            // Create new nested message pump.
            DispatcherFrame nestedFrame = new DispatcherFrame();

            // Dispatch a callback to the current message queue, when getting called,
            // this callback will end the nested message loop.
            // note that the priority of this callback should be lower than the that of UI event messages.
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(

                                                  DispatcherPriority.Background, exitFrameCallback, nestedFrame);

            // pump the nested message loop, the nested message loop will
            // immediately process the messages left inside the message queue.
            Dispatcher.PushFrame(nestedFrame);

            // If the "exitFrame" callback doesn't get finished, Abort it.
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }

        static Object ExitFrame(Object state)
        {
            DispatcherFrame frame = state as DispatcherFrame;

            // Exit the nested message loop.
            frame.Continue = false;

            return null;
        }
    }
}
